/********************************************************************
 
 Copyright (C), 2012年 dhc. All rights reserved.
 
 Filename: 	    MailListCell.h 
 
 Author:        muzongcun
 
 Version:       1.0
 
 Description:   MailListCell
 
 History:
 
 muzongcun    2012/3/10     1.0     Create
 
 *********************************************************************/
#import "MailListCell.h"
#import "MailList.h"
#import "XmlRpc.h"

@implementation MailListCell
@synthesize     mailsubject;
@synthesize     mailtime;
@synthesize     mailaddress;
@synthesize     mailCheckBox;
@synthesize     isChecked;
@synthesize     _mailList;
@synthesize     mailImportantFlag;
@synthesize     isImportantFlag;
@synthesize     mailContent;
@synthesize     mailAttachImage;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
    }
    return self;
}
- (void)viewDidLoad
{
}
//******************************************************
// Method:     setSelected
// Access:     public 
// Returns:    void
// Parameter:  selected
// Parameter:  animated
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)dealloc
{
    [mailtime release];
    [mailaddress release];
    [mailsubject release];
}

//******************************************************
// Method:     setIsChecked
// Access:     public 
// Returns:    void
// Parameter:  check
// Note:	   属性isCheced函数，选中checkbox
//             设置选中，和解除图片
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)setIsChecked:(BOOL)check
{       
    NSString *cellCount = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d",_mailList.m_checkedArray.count]];
    
	isChecked=check;
	if (check) 
    {
		[self.mailCheckBox setImage:[UIImage imageNamed:PIC_MAIL_SELECT] forState:UIControlStateNormal];
        self.backgroundView.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
        
        [_mailList.m_mailNumTotal setTitle:cellCount forState:UIControlStateNormal];
        [_mailList onShow];
		
	}
	else 
    {
		[self.mailCheckBox setImage:[UIImage imageNamed:PIC_MAIL_NO_SELECT] forState:UIControlStateNormal];
         self.backgroundView.backgroundColor = [UIColor clearColor];
        [_mailList.m_mailNumTotal setTitle:cellCount forState:UIControlStateNormal];
	}
    [cellCount release];
}

//******************************************************
// Method:     checkBoxClicked
// Access:     public 
// Returns:    IBAction
// Parameter:  void
// Note:	   点击checkbox，根据点击的cell，返回当前
//             的tag，判断是那一行，并保存选中的celltag
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(IBAction) checkBoxClicked
{
    int row = self.tag;
    BOOL bFlag = TRUE;
    NSString* strTag = [NSString stringWithFormat:@"%d",row]; 
	if(self.isChecked == NO)
    {
         NSMutableArray *tmpArray = _mailList.m_checkedArray;
        NSMutableDictionary *temDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
        for (NSMutableDictionary* tagDic in tmpArray)
        {
            NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
            if ([strCellTag compare:strTag]==0)
            {
                bFlag = FALSE;
                break;
            }
        }
        if (bFlag) 
        {
            [temDictionary setObject:strTag forKey:@"cellTag"];                                        
            [_mailList.m_checkedArray addObject:temDictionary];
        }
        [temDictionary release];
        self.isChecked =YES;
	}else
    {
        NSMutableArray *tmpArray = _mailList.m_checkedArray;
        for (NSMutableDictionary* tagDic in tmpArray)
        {
            NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
            if ([strCellTag compare:strTag] == 0) 
            {
                [tmpArray removeObject:tagDic];
                //[tagDic release];
                break;
            }
        }
        if ([tmpArray count] == 0) 
        {
            _mailList.m_toolDrawerView.hidden = YES;
        }
		self.isChecked =NO;
	}
	//[target performSelector:fun];
}

//******************************************************
// Method:     setIsImportFlag
// Access:     public 
// Returns:    void
// Parameter:  importFlag
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)setIsImportantFlag:(BOOL)importantFlag
{
	isImportantFlag = importantFlag;
	if (importantFlag) 
    {
		[self.mailImportantFlag setImage:[UIImage imageNamed:PIC_MAIL_IMPORTANCE] forState:UIControlStateNormal];
	}
	else 
    {
		[self.mailImportantFlag setImage:[UIImage imageNamed:PIC_MAIL_NO_IMPORTANCE] forState:UIControlStateNormal];
	}
}

//******************************************************
// Method:     mailImportFlagClicked
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   初始化后第一次调用
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(IBAction) mailImportantFlagClicked
{
    int row = self.tag;
    
    NSMutableDictionary * listDic;
    if (row < [_mailList.m_mutableArray count]) 
    {
        listDic = [ _mailList.m_mutableArray objectAtIndex:row]; 
    }else
    {
        listDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    }
    
    //取得mailiid
    NSString* shareID = [listDic  objectForKey:@"id"];
    
    NSString* strTag = [NSString stringWithFormat:@"%d",row]; 
    BOOL bImportantFlag = [self isHaveImportantFlag:strTag];
    
    self.isImportantFlag = bImportantFlag;
    
	if(self.isImportantFlag == NO)
    {
        NSMutableDictionary *temDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
 
        [temDictionary setObject:strTag forKey:@"cellTag"];                                        
        [_mailList.m_importantArray addObject:temDictionary];
        [temDictionary release];
        self.isImportantFlag = YES;
	}
    else
    {
        NSMutableArray *tmpArray = _mailList.m_importantArray;
        for (NSMutableDictionary* tagDic in tmpArray)
        {
            NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
            if ([strCellTag compare:strTag] == 0) 
            {
                [tmpArray removeObject:tagDic];
                //[tagDic release];
                break;
            }
        }
		self.isImportantFlag = NO;
	}
    
    
    
    NSString* strImportantantFlag = [NSString stringWithFormat:@"%d",isImportantFlag]; 
    XmlRpc *xml = [[XmlRpc alloc] init];
    BOOL bSuccess = [xml settingMailImportantFlag:shareID employId:_mailList.m_employeeID importantFlag:strImportantantFlag]; 
    
    if (bSuccess) 
    {
        if (row < [_mailList.m_mutableArray count]) 
        {
            listDic = [_mailList.m_mutableArray objectAtIndex:row];
            [listDic setObject:strImportantantFlag forKey:@"importantflag"]; 
        }
    }
    [xml release];
    //NSLog(@"%@",_mailList.m_mutableArray);
}

//******************************************************
// Method:     isHaveImportFlag
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   初始化后第一次调用
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (BOOL) isHaveImportantFlag:(NSString*)strTag
{
    NSMutableArray *importantArray = _mailList.m_importantArray;
    for (NSMutableDictionary* tagDic in importantArray)
    {
        NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
        if ([strCellTag compare:strTag]==0)
        {
            return TRUE;
        }
    }
    return  FALSE;
}

//******************************************************
// Method:     setMailListObject
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   初始化后第一次调用
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)setMailListObject:(MailList*) mailList
{
    _mailList = mailList;
}
@end
